"""
YAML 文件 读取器
"""

import logging
from typing import (
    Any,
    Dict,
    List,
    Union,
)

from yaml import (
    load,
    FullLoader,
)

__all__ = [
    "read_yaml",  # ->              解析 YAML 字符串
    "read_yaml_from_path",  # ->    解析 YAML（根据YAML 文件路径）
]

logger = logging.getLogger()


def read_yaml(string: str) -> Union[List[dict], Dict[str, Any]]:
    """
    解析 YAML 字符串

    :param string:  YAML 字符串
    :return:        解析后的数据
    """

    try:
        return load(string, Loader=FullLoader)

    except Exception as e:
        logger.error(f"【解析 YAML 字符串】错误：{e}")


def read_yaml_from_path(path: str) -> Union[List[dict], Dict[str, Any]]:
    """
    解析 YAML（根据YAML 文件路径）

    :param path:    YAML 文件的路径
    :return:        解析后的数据
    """

    try:
        with open(path, "r", encoding="UTF-8") as f:
            return read_yaml(f.read())

    except Exception as e:
        logger.error(f"【解析 YAML（根据YAML 文件路径）】错误：{e}")
